package cn.cs.service.impl;

import cn.cs.client.AgentClient;
import cn.cs.client.ComplaintClient;
import cn.cs.client.RestAppOrderClient;
import cn.cs.common.*;
import cn.cs.pojo.Agent;
import cn.cs.pojo.Complaint;
import cn.cs.pojo.CsPerson;
import cn.cs.service.ComplaintService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Resource
    private ComplaintClient complaintClient;

    @Resource
    private AgentClient agentClient;




    @Override
    public Complaint getComplaintById(String id)throws Exception{
        return complaintClient.getComplaintUserById(id);
    }

    @Override
    public List<Complaint> getComplaintByPhone(String userId)throws Exception{
        return complaintClient.getComplaintByPhone(userId);
    }

    @Override
    public List<Complaint>	getComplaintListByMap(Map<String,Object> param)throws Exception{
        return complaintClient.getComplaintListByMap(param);
    }
    @Override
    public Integer getComplaintCountByMap(Map<String,Object> param)throws Exception{
        return complaintClient.getComplaintCountByMap(param);
    }

    public Integer itriptxAddComplaint(Complaint complaint)throws Exception{
            //complaint.setCreationDate(new Date());
            return complaintClient.itriptxAddComplaint(complaint);
    }

    public Integer itriptxModifyComplaint(Complaint complaint)throws Exception{
        //complaint.setModifyDate(new Date());
        return complaintClient.itriptxModifyComplaint(complaint);
    }
    @Override
    public Integer itriptxDeleteComplaintById(Long id)throws Exception{
        return complaintClient.itriptxDeleteComplaintById(id);
    }
    @Override
    public Page<Complaint> queryComplaintPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = complaintClient.getComplaintCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Complaint> complaintList = complaintClient.getComplaintListByMap(param);
        for(Complaint a:complaintList){
            String invitation_code = a.getInvitation_code();
            Agent agent = agentClient.getAgentByCode(invitation_code);
            a.setName(agent.getName());
            a.setPerson(agent.getPerson());
            a.setAgentPhone(agent.getPhone());
        }
        page.setRows(complaintList);
        return page;
    }

    @Override
    public Dto registerComplaint(HttpServletRequest request, String clientUserId, String title, String content, String createTime) throws Exception {
        Complaint complaint=new Complaint();
        complaint.setId(IdWorker.getId());
        complaint.setClientUserId(clientUserId);
        complaint.setState("未处理");
        complaint.setTitle(title);
        complaint.setContent(content);
        complaintClient.itriptxAddComplaint(complaint);
        return DtoUtil.returnSuccess("操作成功");
    }


    @Override
    public Map<String, Object> getAgentByRepairUserId(String id) throws Exception {
        return complaintClient.getAgentByRepairUserId(id);
    }

    @Override
    public Map<String, Object> getAgentABByRepairUserId(String id) throws Exception {
        return complaintClient.getAgentABByRepairUserId(id);
    }

    @Override
    public Map<String, Object> getAgentABByOrderId(String id) throws Exception {
        return complaintClient.getAgentABByOrderId(id);
    }

    @Override
    public Map<String, Object> getAgentABByAppOrderId(String id) throws Exception {
        return complaintClient.getAgentABByAppOrderId(id);
    }



}
